package com.swinw.asumall.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.swinw.common.vaild.AddGroup;
import com.swinw.common.vaild.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.swinw.asumall.product.entity.BrandEntity;
import com.swinw.asumall.product.service.BrandService;
import com.swinw.common.utils.PageUtils;
import com.swinw.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author 罗义华
 * @email 2407550328@qq.com
 * @date 2020-10-04 12:33:33
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     *
     * @param bindingResut 校验结果
     * @Valid :开启校验规则
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand/*, BindingResult bindingResult*/) {
//        //校验数据
//        if(bindingResult.hasErrors()){
//            Map<String,String> map = new HashMap<>();
//            bindingResult.getFieldErrors().forEach((item)->{
//                String message = item.getDefaultMessage();//错误消息
//                String field = item.getField();//引起错误的字段名
//                map.put(field,message);
//            });
//            return R.error(400,"提交的参数不合法").put("data",map);
//        }else{
        brandService.save(brand);
        return R.ok();
//        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 修改品牌的显示状态
     *
     * @param brand
     * @return
     */
    @RequestMapping("update/status")
    //@RequiresPermissions("product:brand:update")
    public R updateStatus(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
